package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.dto.SysUserIpbindDTO;
import org.byqh.dqeai.entity.SysUserIpbind;
import org.byqh.dqeai.entity.proxy.SysUserIpbindProxy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sysUserIpbind")
@Tag(name = "系统[SysUserIpbind] - 用户IP绑定管理")
public class SysUserIpbindController extends BaseController<SysUserIpbind, SysUserIpbindProxy> {
    /**
     * 分页查询数据
     */
    @PostMapping(value = "/list")
    public Json<List<SysUserIpbind>> listAll(@RequestBody(required = false) SysUserIpbindDTO dto) {
        return this.listImpl(SysUserIpbind.class, (query) -> {
            if (!StringUtils.isBlank(dto.getKeyword())) {
                String sqlUser = String.format(
                        "select user_id from eai_sys_user " +
                                " where user_code like '%%%s%%' " +
                                " or user_name like '%%%s%%' " +
                                " or user_email like '%%%s%%' "
                        , dto.getKeyword(), dto.getKeyword(), dto.getKeyword());

                query = query.where(o -> o.userId().in(easyQueryClient.queryable(sqlUser, String.class)));
            }

            return query;
        });
    }

    /**
     * 删除数据
     */
    @PostMapping(value = "/delete")
    public Json delete(@RequestBody SysUserIpbindDTO dto) {
        try {
            if (dto.getIdList() == null || dto.getIdList().size() == 0) {
                return Json.error();
            }

            this.getEntityDeletable()
                    .where(s -> s.id().in(dto.getIdList()));
        } catch (Exception ex) {
            ex.printStackTrace();

            return Json.exception(ex);
        }

        return Json.success();
    }
}